package cn.formaggie.controller.manage;

import cn.formaggie.annotation.CheckTwiceSubmit;
import cn.formaggie.controller.BaseController;
import cn.formaggie.entity.model.PageBean;
import cn.formaggie.entity.model.order.OrderItem;
import cn.formaggie.service.face.OrderItemService;
import cn.formaggie.service.face.OrderService;
import cn.formaggie.service.face.SampleService;
import cn.formaggie.service.face.StoreItemService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/back/orderitem/byorder")
public class OrderItemManageAjaxController extends BaseController {

    @Autowired
    private SampleService sampleService;
    @Autowired
    private OrderItemService orderItemService;

    @RequiresPermissions({"订单详情管理"})
    @RequestMapping("/list/{orderId}/{code}")
    public ModelAndView list(@PathVariable("orderId") Integer orderId,
                             @PathVariable("code") String code,
                             @ModelAttribute(STATE_MSG)String stateMsg){
        ModelAndView mav=new ModelAndView();
        mav.addObject(STATE_MSG,stateMsg);
        mav.addObject("orderId",orderId);
        mav.addObject("code",code);
        mav.addObject("samples",sampleService.getAllSamples());
        mav.setViewName("/backpage/manager/order/items");
        return mav;
    }

    @RequiresPermissions({"订单详情管理"})
    @RequestMapping("/load/{orderId}")
    @ResponseBody
    public Map load(@PathVariable(value = "orderId")Integer orderId,
                    @RequestParam(value = "page") Integer page,
                    @RequestParam(value = "limit") Integer limit){

        List<OrderItem> data=orderItemService.getOrderItemsByOrderId(orderId);
        Integer count=data.size();

        Map<String,Object> viewMap=new HashMap<>();
        viewMap.put("data",data);
        viewMap.put("count",count);
        viewMap.put("code",0);
        viewMap.put("msg","");
        return viewMap;
    }

    @RequiresPermissions({"订单详情管理"})
    @RequestMapping("/add")
    @CheckTwiceSubmit
    public String addOrderItem(OrderItem orderItem, RedirectAttributes model){
        Boolean state=orderItemService.addOrderItem(orderItem);
        model.addFlashAttribute(STATE_MSG,state?"添加成功":"添加失败");
        return "redirect:/back/orderitem/byorder/list/"+orderItem.getOrder().getId()+"/"+orderItem.getOrder().getCode();
    }

    @RequiresPermissions({"订单详情管理"})
    @RequestMapping("/modifyInfo")
    @CheckTwiceSubmit
    public String modifyOrderItemInfo(OrderItem orderItem,RedirectAttributes model){
        Boolean state= orderItemService.modifyOrderItemInfo(orderItem);
        model.addFlashAttribute(STATE_MSG,state?"修改成功":"修改失败");
        return "redirect:/back/orderitem/byorder/list/"+orderItem.getOrder().getId()+"/"+orderItem.getOrder().getCode();
    }

    @RequiresPermissions({"订单详情管理"})
    @RequestMapping("/delete")
    @ResponseBody
    public boolean deleteOrderItem(Integer id){
        return orderItemService.deleteOrderItem(id);
    }

}
